import {get, post, upload} from "./http";
const FormData = require('form-data');

export const _get = function(url, data, success, error) {
  get(url, data)
    .then(res => {
      success(res);
    })
    .catch(err => {
      error && error(err);
    });
};

export const _post = function (url, data, success, error) {
  post(url, data).then(res => {
    success(res);
  })
    .catch(err => {
      console.log(error);
      error && error(err);
    });
};

export const logout = (success, error) => {
  _post('ajax/logout', {}, success, error)
}

export const teacherModel = {
  pre: '/user/teacher',
  list: function (data, success, error) {
    _get(this.pre + 's', data, success, error)
  },
  create: function(data, success, error) {
    _post(this.pre + '/create', data, success, error)
  },
  update: function(id, data, success, error) {
    _post(this.pre + '/update/' + id, data, success, error)
  },
  batchBlock: function(data, success, error) {
    _post('/user/teacher/batch/block', data, success, error)
  },
  item:function(data, success, error) {
    _get(this.pre + '/view/' + data, {}, success, error)
  },
}

export const studentModel =  {
  pre: '/user/student',
  list: function(data, success, error) {
    _get(this.pre + 's', data, success, error)
  }, 
  item:function(data, success, error) {
    _get(this.pre + '/view/' + data, {}, success, error)
  },
  courses: function(success, error) {
    _get('/object/courses', {}, success, error)
  }, 
  exams: function(data, success, error) {
    _get('object/exam/student/' + data.course_id + '/' + data.student_id, {}, success, error)
  }, 
  experiments: function(data, success, error) {
    _get('object/experiment/student/' + data.course_id + '/' + data.student_id, {}, success, error)
  },
  create: function(data, success, error) {
    _post(this.pre + '/create', data, success, error)
  },
  update: function(id, data, success, error) {
    _post(this.pre + '/update/' + id, data, success, error)
  }
}

export const expertModel =  {
  pre: '/user/expert',
  list: function(data, success, error) {
    _get(this.pre + 's', data, success, error)
  }, 
  create: function(data, success, error) {
    _post(this.pre + '/create', data, success, error)
  },
  update: function(id, data, success, error) {
    _post(this.pre + '/update/' + id, data, success, error)
  },
  batchBlock: function(data, success, error) {
    _post('/user/expert/batch/block', data, success, error)
  },
  item:function(data, success, error) {
    _get(this.pre + '/view/' + data, {},  success, error)
  }
}

export const collegeModel = {
  pre: '/object/college',
  list: function(data, success, error) {
    _get('/college/list', data, success, error)
  },
  objectList: function(data, success, error) {
    _get('/object/colleges', data, success, error)
  },
  create: function(data, success, error) {
    _post(this.pre + '/create', data, success, error)
  },
  update: function(id, data, success, error) {
    _post(this.pre + '/update/' + id, data, success, error)
  },
  item: function(id, success, error) {
    _get(this.pre + '/' + id, {}, success, error)
  }
}

export const schoolModel = {
  pre: '/object/school',
  list: function(data, success, error) {
    _get('/school/list', data, success, error)
  },
  objectList: function(data, success, error) {
    _get('/object/schools', data, success, error)
  },
  create: function(data, success, error) {
    _post(this.pre + '/create', data, success, error)
  },
  update: function(id, data, success, error) {
    _post(this.pre + '/update/' + id, data, success, error)
  },
  item: function(id, success, error) {
    _get(this.pre + '/' + id, {}, success, error)
  },
 
}

export const majorModel = {
  pre: '/object/major',
  list: function(data, success, error) {
    _get('/major/list', data, success, error)
  },
  objectList: function(data, success, error) {
    _get('/object/majors', data, success, error)
  },
  create: function(data, success, error) {
    _post(this.pre + '/create', data, success, error)
  },
  update: function(id, data, success, error) {
    _post(this.pre + '/update/' + id, data, success, error)
  },
  item: function(id, success, error) {
    _get(this.pre + '/' + id, {}, success, error)
  }
}

export const classModel = {
  pre: '/object/class',
  search: function(data, success, error) {
    _get('/class/list', data, success, error)
  },
  list: function(data, success, error) {
    _get(this.pre + 'es', data, success, error)
  },
  create: function(data, success, error) {
    _post(this.pre + '/create', data, success, error)
  },
  update: function(id, data, success, error) {
    _post(this.pre + '/update/' + id, data, success, error)
  },
  batchBlock: function(data, success, error) {
    _post('/object/class/batch/block', data, success, error)
  },
  batchAdd: function(data, success, error) {
    _post('/batch/add', data, success, error)
  },
  item: function(id, success, error) {
    _get(this.pre + '/' + id, {}, success, error)
  } 
}

export const courseModel = {
  pre: '/object/course',
  list: function(data, success, error) {
    _get(this.pre + 's', data, success, error)
  },
  chapters: function(course_id, success, error) {
    _get(this.pre + '/chapter/' + course_id, {}, success, error)
  },
  chapterAdd: function(data, success, error) {
    _post(this.pre + '/chapter/add', data, success, error)
  },
  create: function(data, success, error) {
    _post(this.pre + '/create', data, success, error)
  },
  item: function(data, success, error) {
    _get(this.pre + '/' + data, {}, success, error)
  },
  update: function(id, data, success, error) {
    _post(this.pre + '/update/' + id, data, success, error)
  },
  batchBlock: function(data, success, error) {
    _post('/object/course/batch/block', data, success, error)
  },
  chapter: function(data, success, error) {
    _get('/object/chapter/' + data, {}, success, error)
  },
  batchBlockChapter: function(data, success, error) {
    _post('/object/chapter/batch/block', data, success, error)
  },
  chapterUpdate: function(id, data, success, error) {
    _post('/object/chapter/update/' + id, data, success, error)
  },
  students: function(id, data, success, error) {
    _get(this.pre + '/student/' + id, data, success, error)
  },
  teachers: function(id, success, error) {
    _get(this.pre + '/teacher/' + id, {}, success, error)
  },
  studentAdd: function(data, success, error) {
    _post(this.pre + '/student/add', data, success, error)
  },
  teacherAdd: function(data, success, error) {
    _post(this.pre + '/teacher/add', data, success, error)
  }
}

export function _upload(file, success, error) {
  const form = new FormData();
  form.append('location', file);
  upload('/object/richtext/image/upload', form).then(res => {
    success(res);
  }).catch(err => {
    error(err)
  })
}

export const experimentModel = {
  pre: '/object/experiment',
  list: function(data, success, error) {
    _get(this.pre + 's', data, success, error)
  },
  envs: function(data, success, error) {
    _get('/object/environments', data, success, error)
  },
  item: function(data, success, error) {
    _get(this.pre + '/' + data, {} , success, error)
  },
  env: function(data, success, error) {
    _get('/object/environment/' + data, {}, success, error)
  },
  envIns: function(data, success, error) {
    _get('/object/environment/instance/' + data, {}, success, error)
  },
  insBatchRec: function(data, success, error) {
    _post('/object/instance/batch/recovery', data, success, error)
  },
  insBatchSd: function(data, success, error) {
    _post('/object/instance/batch/shutdown', data, success, error)
  },
  insBatchStartUp: function(data, success, error) {
    _post('/object/instance/batch/startup', data, success, error)
  },
  instBatchCreate: function(data, success, error) {
    _post('/object/instance/batch/create', data, success, error)
  },
  envIssue: function(data, success, error) {
    _post('/object/environment/issue', data, success, error)
  },
  reports: function(cid, sid, success, error) {
    _get(this.pre + `/student/${cid}/${sid}`, {}, success, error)
  },
  create: function(data, success, error) {
    _post('object/experiment/create', data, success, error)
  },
  update: function(data, success, error) {
    _post('object/experiment/update/' + data.id, data, success, error)
  },
  updateDesc: function(data, success, error) {
    _post('object/experiment/update/desc/' + data.id, data, success, error)
  },
  setStatus:  function(data, success, error) {
    _post('object/experiment/status/set', data, success, error)
  },
  setShow:  function(data, success, error) {
    _post('object/experiment/show/set', data, success, error)
  },
  ReSetPassWord:  function(data, success, error) {
    _post('object/resetpassword', data, success, error)
  },
}

export const imageModel = {
  pre: '/object/image',
  list: function(data, success, error) {
    _get(this.pre + 's', data, success, error)
  },
  search: function(data, success, error) {
    _get(this.pre + '/list', data, success, error)
  },
  upload: function(data, success, error) {
    _post(this.pre + '/upload', data, success, error)
  },
  import: function(data, success, error) {
    _post(this.pre + '/import', data, success, error)
  },
  createImport: function(data, success, error) {
    _post(this.pre + '/createimport', data, success, error)
  },
  describe: function(data, success, error) {
    _post(this.pre + '/describe', data, success, error)
  },
  create1: function(data, success, error) {
    _post(this.pre + '/create/first', data, success, error)
  },
  create2: function(data, success, error) {
    _post(this.pre + '/create/second', data, success, error)
  },
  create3: function(data, success, error) {
    _post(this.pre + '/create/third', data, success, error)
  },
  item: function(data, success, error) {
    _get(this.pre + '/' + data, {}, success, error)
  },
  batchRem: function(data, success, error) {
    _post(this.pre + '/batch/remove', data, success, error)
  },
  tempInstances: function(data, success, error) {
    _get('/object/tempinstances', data, success, error)
  },
  delMyImage: function(data, success, error) {
    _post('object/my/image/del', data, success, error)
  },

  AsyncImage: function(data, success, error) {
    _post('object/my/image/async', data, success, error)
  },
}

export const kldModel = {
  pre: '/object/knowledge',
  search: function(data, success, error) {
    _get(this.pre + 's/search', data, success, error)
  },
  list: function(success, error) {
    _get(this.pre + 's', {}, success, error)
  },
  create: function(data, success, error) {
    _post(this.pre + '/create', data, success, error)
  }
}

export const examModel = {
  pre: "/object/exam",
  list: function(data, success, error) {
    _get(this.pre + 's', data, success, error)
  },
  autoCreate: function(data, success, error) {
    _post('object/exam/autocreate', data, success, error)
  },
  manualCreate: function(data, success, error) {
    _post('object/exam/manualcreate', data, success, error)
  },
  item: function(data, success, error) {
    _get(this.pre + '/' + data, {}, success, error)
  },
  addQues: function(data, success, error) {
    _post('object/examquestion/add', data, success, error)
  },
  remQues: function(data, success, error) {
    _post('object/examquestion/remove', data, success, error)
  },
  update: function(data, success, error) {
    _post('object/exam/update/' + data.id, data, success, error)
  },
  action: function(data, success, error) {
    _post('object/exam/action/' + data.id, data, success, error)
  },
}

export const envModel = {
  pre: "/object/environment",
  list: function(data, success, error) {
    _get(this.pre + 's', data, success, error)
  },
  create: function(data, success, error) {
    _post('/object/environment/create', data, success, error)
  },
  update: function(data, success, error) {
    _post('/object/environment/update/' + data.id, data, success, error)
  },
  batchRem: function(data, success, error) {
    _post(this.pre + '/batch/remove', data, success, error)
  },
  item: function(data, success, error) {
    _get('/object/environment/' + data, {}, success, error)
  },
  getByExperiment: function(data, success, error) {
    _get('/object/experiment/env/' + data, {}, success, error)
  },
}

export const queModel = { 
  pre: "/object/question",
  list: function(data, success, error) {
    _get(this.pre + 's', data, success, error)
  },
  // 新建填空题BL，解答题SA
  createBlank: (data, success, error) => {
    _post('object/blankquestion/create', data, success, error)
  },
  createChoice: (data, success, error) => {
    _post('object/choicequestion/create', data, success, error)
  }, 
  updateBlank: (data, success, error) => {
    _post('object/blankquestion/update/' + data.id, data, success, error)
  },
  updateChoice: (data, success, error) => {
    _post('object/choicequestion/update/' + data.id, data, success, error)
  },
  blankQuesItem: (id, success, error) => {
    _get('object/blankquestion/' + id, {}, success, error)
  },
  choiceQuesItem: (id, success, error) => {
    _get('object/choicequestion/' + id, {}, success, error)
  },
  batchRem: (data, success, error) => {
    _post('object/question/batch/remove', data, success, error)
  }
}

export const reportModel = { 
  pre: "/object/report",
  list: function(data, success, error) {
    _get(this.pre + 's', data, success, error)
  },
  item:  function(data, success, error) {
    _get(this.pre + '/' + data, {}, success, error)
  },
  score: function(data, success, error) {
    _post(this.pre + '/score/' + data.id, data, success, error)
  }
}

export const paperModel = {
  pre: "/object/paper",
  list: function(data, success, error) {
    _get(this.pre + 's', data, success, error)
  },
  item: function(data, success, error) {
    _get(this.pre + '/' + data, {}, success, error)
  },
  questionScore: function(data, success, error) {
    _post('object/paperquestion/update/' + data.id, data, success, error)
  },
}

export const acmModel = {
  getTk: function(success, error) {
    _get('sso/getTk/', {}, success, error)
  },
}

export const cosModel = {
  getSTS: function(success, error) {
    _get('cos/sts', {}, success, error)
  },
  getSign: function(data, success, error) {
    _get('cos/sign', data, success, error)
  }
}

export const eventModel = {
  pre: "/webevent/",
  list: function(data, success, error) {
    _get(this.pre + 'tea/list', data, success, error)
  }
}

export const insModel = {
  createImage: function(data, success, error) {
    _post('ins/create/image', data, success, error)
  },
  action: function(data, success, error) {
    _post('object/tempins/action', data, success, error)
  },
  cancel: function(data, success, error) {
    _post('object/tempins/cancel', data, success, error)
  },
}

export const govModel = {
  create: function(data, success, error) {
    _post('gov/create', data, success, error)
  },
  update: function(id, data, success, error) {
    _post('gov/update/' + id, data, success, error)
  },
  item: function(id, success, error) {
    _get('gov/' + id, {}, success, error)
  },
  del: function(data, success, error) {
    _post('gov/del', data, success, error)
  },
  list: function(data, success, error) {
    _get('gov/list', data, success, error)
  },
  govCourseCreate: function(data, success, error) {
    _post('gov/course/create', data, success, error)
  },
  govCourseList: function(data, success, error) {
    _get('gov/course/list', data, success, error)
  }
}